/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modele;

import ejb.Employe;
import ejb.EmployeModelRemote;
import exceptions.DataSourceException;
import exceptions.EmployeNotFoundException;
import exceptions.LoginException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Trasheur
 */
public class EmployeModel implements EmployeModelRemote {

    private EmployeModelRemote employeModel = lookupEmployeModelRemote();
    private ConnectionFactory connectionFactory = lookupConnectionFactory();
    private Queue queue = lookupQueue();
    private String factory = null;

    public String getConnectionFactory() {
        return factory;
    }

    public void setConnectionFactory(String factory) {
        this.factory = factory;
    }

    private ConnectionFactory lookupConnectionFactory() {
        try {
            Context c = new InitialContext();
            return (ConnectionFactory) c.lookup("jms/NewsMessageFactory");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private Queue lookupQueue() {
        try {
            Context c = new InitialContext();
            return (Queue) c.lookup("jms/NewsMessage");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private EmployeModelRemote lookupEmployeModelRemote() {
        try {
            Context c = new InitialContext();
            return (EmployeModelRemote) c.lookup("ejb/TP9EJB");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    @Override
    public Employe get(String username) throws EmployeNotFoundException, DataSourceException {
        return employeModel.get(username);
    }

    @Override
    public void create(Employe employe) throws DataSourceException {
        employeModel.create(employe);
    }

    @Override
    public void createAsync(Employe employe) throws DataSourceException {
        Connection connection = null;
        Session session = null;
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(queue);
            ObjectMessage message = session.createObjectMessage();
            message.setObject(employe);
            messageProducer.send(message);
            messageProducer.close();
            session.close();
            session = null;
            connection.close();
            connection = null;
        } catch (JMSException ex) {
            throw new DataSourceException();
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    throw new DataSourceException();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException ex) {
                    throw new DataSourceException();
                }
            }
        }
    }

    @Override
    public List<Employe> getAll() throws DataSourceException {
        return employeModel.getAll();
    }

    @Override
    public void delete(String username) throws DataSourceException {
        employeModel.delete(username);
    }

    @Override
    public void update(Employe employe) throws DataSourceException {
        employeModel.update(employe);
    }

    @Override
    public String logon(String username, String password) throws LoginException, DataSourceException {
        return employeModel.logon(username, password);
    }
}
